package com.tensquare.article.controller;

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommentService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CommentController
 * @Description TODO
 * @Author zhangshuai
 * @Date 2019/1/16 13:58
 * @Version 1.0
 **/

@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 增加评论
     *
     * @param comment
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Comment comment) {
        commentService.add(comment);
        return new Result(true, StatusCode.OK, "提交成功");
    }

    /**
     * 根据文章ID查询评论
     *
     * @param articleId
     * @return
     */
    @RequestMapping(value = "/{articleId}", method = RequestMethod.GET)
    public Result findByArticleid(@PathVariable String articleId) {
        List<Comment> list = commentService.findByArticleid(articleId);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 删除
     *
     * @param articleId
     * @return
     */
    @RequestMapping(value = "/{articleId}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String articleId) {
        commentService.delete(articleId);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
